<?php


namespace IDock\Insurance\ZYIC;

use IDock\Http\Response;
use IDock\IDock\IDockInterface;
use IDock\Insurance\ZYIC\Request\CancelPaymentRequest;
use IDock\Insurance\ZYIC\Request\ElectricalSignRequest;
use IDock\Insurance\ZYIC\Request\EPolicyDownloadRequest;
use IDock\Insurance\ZYIC\Request\NonCarInquiryRequest;
use IDock\Insurance\ZYIC\Request\NonCarPolicyInfoRequest;
use IDock\Insurance\ZYIC\Request\NonCarUnderwritingRequest;
use IDock\Insurance\ZYIC\Request\PolicyInfoRequest;
use IDock\Insurance\ZYIC\Request\PolicyPaymentRequest;
use IDock\Insurance\ZYIC\Request\PremiumInquiryRequest;
use IDock\Insurance\ZYIC\Request\ProposalRecallRequest;
use IDock\Insurance\ZYIC\Request\ProposalStatusRequest;
use IDock\Insurance\ZYIC\Request\RenewalInquiryRequest;
use IDock\Insurance\ZYIC\Request\SaveOrderRequest;
use IDock\Insurance\ZYIC\Request\UnderwritingReasonRequest;
use IDock\Insurance\ZYIC\Request\UnderwritingRequest;
use IDock\Insurance\ZYIC\Request\UploadFileRequest;
use IDock\Insurance\ZYIC\Request\ValidateCarRequest;
use IDock\Insurance\ZYIC\Request\VehicleActualValueRequest;
use IDock\Insurance\ZYIC\Request\VehicleModelRequest;
use IDock\Insurance\ZYIC\Response\ElectricalSignResponse;
use IDock\Insurance\ZYIC\Response\EPolicyDownloadResponse;
use IDock\Insurance\ZYIC\Response\NonCarInquiryResponse;
use IDock\Insurance\ZYIC\Response\NonCarPolicyInfoResponse;
use IDock\Insurance\ZYIC\Response\NonCarProductListResponse;
use IDock\Insurance\ZYIC\Response\NonCarUnderwritingResponse;
use IDock\Insurance\ZYIC\Response\PolicyInfoResponse;
use IDock\Insurance\ZYIC\Response\PolicyPaymentResponse;
use IDock\Insurance\ZYIC\Response\PremiumInquiryResponse;
use IDock\Insurance\ZYIC\Response\ProposalRecallResponse;
use IDock\Insurance\ZYIC\Response\ProposalStatusResponse;
use IDock\Insurance\ZYIC\Response\RenewalInquiryResponse;
use IDock\Insurance\ZYIC\Response\SaveOrderResponse;
use IDock\Insurance\ZYIC\Response\UnderwritingReasonResponse;
use IDock\Insurance\ZYIC\Response\UnderwritingResponse;
use IDock\Insurance\ZYIC\Response\UploadFileResponse;
use IDock\Insurance\ZYIC\Response\ValidateCarResponse;
use IDock\Insurance\ZYIC\Response\VehicleActualValueResponse;
use IDock\Insurance\ZYIC\Response\VehicleModelResponse;
use IDock\Visitor\EPolicyDownloadVisitor;
use IDock\Visitor\NonCar\NonCarInquiryVisitor;
use IDock\Visitor\NonCar\NonCarProductListVisitor;
use IDock\Visitor\PolicyInfoVisitor;
use IDock\Visitor\PolicyPaymentVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\Premium\PremiumUnderwritingVisitor;
use IDock\Visitor\ProposalRecallVisitor;
use IDock\Visitor\ProposalStatusVisitor;
use IDock\Visitor\RenewalInquiryVisitor;
use IDock\Visitor\UnderwritingRecallVisitor;
use IDock\Visitor\UploadFileVisitor;
use IDock\Visitor\ValidateCarVisitor;
use IDock\Visitor\VehicleActualValueVisitor;
use IDock\Visitor\VehicleModelVisitor;


class ZYICConstruct extends Builder implements IDockInterface
{
    public $config = [];

    public function __construct($config)
    {
        $this->config = $config;
        $this->notifyUrl = $config['notifyUrl'] ?? '';
        $this->systemFlag = $config['systemFlag'] ?? '';
        $this->planName = $config['planName'] ?? '';
        $this->nonCarGateway = $config['nonCarGateway'] ?? '';
        $this->appKey = $config['appKey'] ?? '';
        $this->channelCode = $config['channelCode'] ?? '';
        $this->publicKey = $config['publicKey'] ?? '';
        $this->privateKey = $config['privateKey'] ?? '';
    }

    /**
     * 续保查询
     * @param RenewalInquiryVisitor $renewalInquiryVisitor
     * @return RenewalInquiryResponse
     */
    public function renewalInquiry(RenewalInquiryVisitor $renewalInquiryVisitor): RenewalInquiryResponse
    {
        $request = new RenewalInquiryRequest();
        $request->setVisitor($renewalInquiryVisitor);
        $renewalInquiryVisitor->argc['riskCode'] = 'DDB';
        /*** @var RenewalInquiryResponse $response */
        $response = $this->execute($request);
        if (!$response->renewFlag) {
            $renewalInquiryVisitor->argc['riskCode'] = 'DDB,DDA';
            $request->setVisitor($renewalInquiryVisitor);
            /*** @var RenewalInquiryResponse $response */
            $response = $this->execute($request);
        }
        return $response;
    }

    /**
     * 车型查询
     * @param VehicleModelVisitor $vehicleModelVisitor
     * @return VehicleModelResponse
     */
    public function vehicleModel(VehicleModelVisitor $vehicleModelVisitor): VehicleModelResponse
    {
        $request = new VehicleModelRequest();
        $request->setVisitor($vehicleModelVisitor);

        /*** @var VehicleModelResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 车辆实际价格
     */
    public function vehicleActualValue(VehicleActualValueVisitor $vehicleActualValueVisitor): Response
    {
        $request = new VehicleActualValueRequest();
        $request->setVisitor($vehicleActualValueVisitor);

        /*** @var VehicleActualValueResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 询价前置
     */
    public function preInquiry(PremiumInquiryVisitor $premiumInquiryVisitor): Response
    {
        return new Response(200);
    }

    public function validateCar(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }

    /**
     * 询价
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return PremiumInquiryResponse|SaveOrderResponse
     */
    public function premiumInquiry(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
        $request = new PremiumInquiryRequest;
        $request->setVisitor($premiumInquiryVisitor);
        /*** @var PremiumInquiryResponse $response */
        $response = $this->execute($request);
        if (!$response->isSuccess() or $response->printer->repeatInsuredFlg) {
            return $response;
        }
        if ($premiumInquiryVisitor->getVehicleVisitor()->isNewEnergy) {
            $response->setNewEnergyCode();
        }
        //非车询价
//        if($premiumInquiryVisitor->getNonCarInquiryVisitor()->getNonCarList()){
//            $nonCarRes = $this->nonCarPremiumInquiry($premiumInquiryVisitor);
//            if(!$nonCarRes->isSuccess()){
//                return $nonCarRes;
//            }
//            $response->printer->argc['channelOrderNo'] = $nonCarRes->channelOrderNo;
//            $response->printer->nonCarInquiryPrinter = $nonCarRes->printer;
//        }

        $response->setExt($premiumInquiryVisitor->getApplicantVisitor()->mobile, $premiumInquiryVisitor->getApplicantVisitor()->typeCode);

        $underwriting = new PremiumUnderwritingVisitor();
        $underwriting->biInquiryNo = $response->printer->biInquiryNo;
        $underwriting->ciInquiryNo = $response->printer->ciInquiryNo;
        $premiumInquiryVisitor->setUnderwritingVisitor($underwriting);
        $request1 = new SaveOrderRequest();
        $request1->setVisitor($premiumInquiryVisitor);

        /*** @var SaveOrderResponse $response1 */
        $response1 = $this->execute($request1);
        if (!$response1->isSuccess()) {
            return $response1;
        }
        $response->printer->argc['proposalNoBI'] = $response1->printer->argc['proposalNoBI'];
        $response->printer->argc['proposalNoCI'] = $response1->printer->argc['proposalNoCI'];
        $response->printer->biInquiryNo = $response1->printer->argc['proposalNoBI'];
        $response->printer->ciInquiryNo = $response1->printer->argc['proposalNoCI'];

        //客户来源回显
        $customerSource = $premiumInquiryVisitor->getExtInfoVisitor()->argc['customerSource'] ?? '05';
        //客户渠道回显
        $InsuredSource = $premiumInquiryVisitor->getExtInfoVisitor()->argc['InsuredSource'] ?? '5';

        $response->printer->argc['customerSource'] = $customerSource;
        $response->printer->argc['InsuredSource'] = $InsuredSource;

        $customerSource = ZYICEnums::$customerSource[$customerSource] ?? '';

        $InsuredSource = ZYICEnums::$InsuredSource[$InsuredSource] ?? '';

        if ($customerSource) {
            $response->printer->premiumFactorPrinter->ComFactorAppend('客户来源', 'customerSource', $customerSource);
        }

        if ($InsuredSource) {
            $response->printer->premiumFactorPrinter->ComFactorAppend('客户渠道', 'InsuredSource', $InsuredSource);
        }

        return $response;
    }

    /**
     * 核保
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return Response
     */
    public function underwriting(PremiumInquiryVisitor $premiumInquiryVisitor): Response
    {
//        $underwritingSuccess = false;
        //非车核保
//        if($premiumInquiryVisitor->getNonCarUnderwritingVisitor()->getNonCarList() && !boolval($premiumInquiryVisitor->getExtInfoVisitor()->argc['underwritingSuccess'] ?? false)){
//            $nonCarRes = $this->nonCarUnderwritingRes($premiumInquiryVisitor);
//            if(!$nonCarRes->isSuccess()){
//                return $nonCarRes;
//            }
//            $underwritingSuccess = true;
//        }
        $request = new UnderwritingRequest();
        $request->setVisitor($premiumInquiryVisitor);

        /*** @var UnderwritingResponse $underwritingResponse */
        $underwritingResponse = $this->execute($request);

        //复写投保单号
        if ($underwritingResponse->printer) {
            $underwritingResponse->printer->ciInquiryNo = $premiumInquiryVisitor->getUnderwritingVisitor()->ciInquiryNo;
            $underwritingResponse->printer->biInquiryNo = $premiumInquiryVisitor->getUnderwritingVisitor()->biInquiryNo;
        }

//        if(!empty($nonCarRes)){
//            $underwritingResponse->printer->argc['underwritingSuccess'] = $underwritingSuccess;
//        }
        return $underwritingResponse;
    }

    /**
     * 支付
     * @param PolicyPaymentVisitor $paymentVisitor
     * @return ElectricalSignResponse|PolicyPaymentResponse
     */
    public function policyPayment(PolicyPaymentVisitor $paymentVisitor)
    {
        $request = new PolicyPaymentRequest();
        $request->setVisitor($paymentVisitor);

        /*** @var PolicyPaymentResponse $response */
        $response = $this->execute($request);
        if (!$response->isSuccess()) {
            return $response;
        }
        if ($paymentVisitor->argc['relationType'] == 'R1') {
            $request1 = new ElectricalSignRequest();
            $request1->setVisitor($paymentVisitor);
            /*** @var ElectricalSignResponse $response1 */
            $response1 = $this->execute($request1);
            $response1->printer->argc = $response->printer->argc;
            return $response1;
        }

        return $response;
    }

    /**
     * 文件上传
     * @param UploadFileVisitor $uploadFileVisitor
     * @return Response
     */
    public function uploadFile(UploadFileVisitor $uploadFileVisitor): Response
    {
        $list = $uploadFileVisitor->getFileList();
        $common = new UploadFileResponse(200);
        foreach ($list as $value) {
            $request = new UploadFileRequest();
            $visitor = new UploadFileVisitor();
            $visitor->argc = $uploadFileVisitor->argc;
            $visitor->inquiryNo = $uploadFileVisitor->inquiryNo;
            $visitor->fileAppend($value['type'], $value['url']);
            $request->setVisitor($visitor);
            /*** @var UploadFileResponse $response */
            $response = $this->execute($request);
        }
        return $response ?? $common;
    }

    /**
     * 投保单状态查询
     * @param ProposalStatusVisitor $proposalStatusVisitor
     * @return ProposalStatusResponse
     */
    public function proposalStatus(ProposalStatusVisitor $proposalStatusVisitor): ProposalStatusResponse
    {
        $request = new ProposalStatusRequest();
        $request->setVisitor($proposalStatusVisitor);
        /*** @var ProposalStatusResponse $response */
        $response = $this->execute($request);
        if ($response->isSuccess() && $response->printer->statusCode == 'U4') {
            $reason = $this->underwritingReason($proposalStatusVisitor);
            if ($reason->isSuccess()) {
                $response->printer->messageTip = $reason->printer->messageTip;
            }
        }
        return $response;
    }

    public function underwritingReason(ProposalStatusVisitor $proposalStatusVisitor): UnderwritingReasonResponse
    {
        $request = new UnderwritingReasonRequest();
        $request->setVisitor($proposalStatusVisitor);
        /*** @var UnderwritingReasonResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 撤单接口
     * @param ProposalRecallVisitor $revokeVisitor
     * @return Response
     */
    public function proposalRecall(ProposalRecallVisitor $revokeVisitor): Response
    {
        $request = new ProposalRecallRequest();
        $request->setVisitor($revokeVisitor);

        /*** @var ProposalRecallResponse $response */
        $response = $this->execute($request);
        if ($response->getError() == '选择的单号已进行缴费登记，请取消登记后再做撤销操作！') {
            $request1 = new CancelPaymentRequest();
            $request1->setVisitor($revokeVisitor);
            $response1 = $this->execute($request1);
            if (!$response1->isSuccess()) {
                return $response1;
            }
            $response = $this->execute($request);
        }
        return $response;
    }

    /**
     * 电子保单下载
     * @param EPolicyDownloadVisitor $EPolicyVisitor
     * @return Response
     */
    public function EPolicyDownload(EPolicyDownloadVisitor $EPolicyVisitor): Response
    {
        $request = new EPolicyDownloadRequest();
        $request->setVisitor($EPolicyVisitor);

        /*** @var EPolicyDownloadResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 核保撤回
     * @param UnderwritingRecallVisitor $recallVisitor
     * @return mixed|void
     */
    public function underwritingRecall(UnderwritingRecallVisitor $recallVisitor): Response
    {
        return new Response(200);
    }

    /**
     * 保单详情
     * @param PolicyInfoVisitor $policyInfoVisitor
     * @return Response
     */
    public function policyInfo(PolicyInfoVisitor $policyInfoVisitor): Response
    {
        $request = new PolicyInfoRequest();
        $request->setVisitor($policyInfoVisitor);

        /*** @var PolicyInfoResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 非车保单号
     * @param PolicyInfoVisitor $policyInfoVisitor
     * @return Response
     */
    public function nonCarPolicyInfo(PolicyInfoVisitor $policyInfoVisitor): Response
    {
        $request = new NonCarPolicyInfoRequest();
        $request->setVisitor($policyInfoVisitor);

        /*** @var NonCarPolicyInfoResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function nonCarProductList(NonCarProductListVisitor $nonCarProductListVisitor): NonCarProductListResponse
    {
        $response = new NonCarProductListResponse(200);
        $response->printerInit();
        return $response;
    }

    public function nonCarPremiumInquiry(PremiumInquiryVisitor $premiumInquiryVisitor): NonCarInquiryResponse
    {
        $request = new NonCarInquiryRequest();
        $request->setVisitor($premiumInquiryVisitor);

        /*** @var NonCarInquiryResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function nonCarInquiry(NonCarInquiryVisitor $nonCarInquiryVisitor): Response
    {
        return new Response(200);
    }

    public function nonCarUnderwritingRes(PremiumInquiryVisitor $premiumInquiryVisitor): NonCarUnderwritingResponse
    {
        $request = new NonCarUnderwritingRequest();
        $request->setVisitor($premiumInquiryVisitor);
        /*** @var NonCarUnderwritingResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    public function nonCarUnderwriting(NonCarInquiryVisitor $inquiryPrinter): Response
    {
        return new Response(200);
    }

    /**
     * 验车码接口
     * @param ValidateCarVisitor $validateCarVisitor
     * @return ValidateCarResponse
     */
    public function validateCarCode(ValidateCarVisitor $validateCarVisitor): ValidateCarResponse
    {
        /*** @var $response ValidateCarResponse */
        $response = $this->execute(new ValidateCarRequest());

        return $response;
    }

    /**
     * 新车备案
     * @return void
     */
    public function newVehicleRegistration(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }
}
